package com.tjetc.view.impl;

import com.tjetc.constant.CommonConstant;
import com.tjetc.exception.MessageException;
import com.tjetc.pojo.dto.BookingDTO;
import com.tjetc.pojo.vo.PayTypeVO;
import com.tjetc.service.PayService;
import com.tjetc.service.impl.PayServiceImpl;
import com.tjetc.utils.ScannerUtils;
import com.tjetc.view.BookingCRUDPage;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @Author: itda
 * @Date: 2024/5/10 21:10
 * @Description: BookingCRUDPageImpl
 */
public class BookingCRUDPageImpl implements BookingCRUDPage {

    private PayService payService = new PayServiceImpl();

    @Override
    public BookingDTO save() {
        System.out.println("请输入预订的房间id:");
        long roomId = ScannerUtils.sc.nextLong();
        System.out.println("请输入预订人的id:");
        long guestId = ScannerUtils.sc.nextLong();
        System.out.println("支付方式包括如下几种:");
        PayTypeVO payTypeVO;
        while (true) {
            payService.list();
            System.out.println("请输入预订人的付款方式:");
            payTypeVO = payService.getByName(ScannerUtils.sc.next());
            if (payTypeVO == null) {
                System.out.println(CommonConstant.COMMAND_ERROR);
            } else {
                break;
            }
        }
        LocalDateTime now = LocalDateTime.now();
        System.out.println("请输入预订人要租住几个小时:");
        int hour = ScannerUtils.sc.nextInt();
        System.out.println("请输入预约入住时间(yyyy-MM-dd):");
        String startDay = ScannerUtils.sc.next();
        System.out.println("请输入预约入住时间(HH:mm:ss):");
        String startTime = ScannerUtils.sc.next();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime dateTime = LocalDateTime.parse(startDay + " " + startTime, formatter);
        if (dateTime.isBefore(now)) {
            try {
                throw new MessageException(CommonConstant.TIME_ERROR);
            } catch (MessageException e) {
                System.out.println(e.getMessage());
            }
        }
        LocalDateTime checkOutDate = dateTime == null ? null : dateTime.plusHours(hour);
        return BookingDTO.builder()
                .roomId(roomId)
                .guestId(guestId)
                .paymentStatus(payTypeVO.getName())
                .checkInDate(dateTime)
                .checkOutDate(checkOutDate)
                .build();
    }

    @Override
    public BookingDTO add() {
        System.out.println("请输入您预订的房间id:");
        long roomId = ScannerUtils.sc.nextLong();
        System.out.println("支付方式包括如下几种:");
        PayTypeVO payTypeVO;
        while (true) {
            payService.list();
            System.out.println("请输入您的付款方式:");
            payTypeVO = payService.getByName(ScannerUtils.sc.next());
            if (payTypeVO == null) {
                System.out.println(CommonConstant.COMMAND_ERROR);
            } else {
                break;
            }
        }
        LocalDateTime now = LocalDateTime.now();
        System.out.println("请输入您要租住几个小时:");
        int hour = ScannerUtils.sc.nextInt();
        System.out.println("请输入您的入住时间(yyyy-MM-dd):");
        String startDay = ScannerUtils.sc.next();
        System.out.println("请输入您的入住时间(HH:mm:ss):");
        String startTime = ScannerUtils.sc.next();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime dateTime = LocalDateTime.parse(startDay + " " + startTime, formatter);
        if (dateTime.isBefore(now)) {
            try {
                throw new MessageException(CommonConstant.TIME_ERROR);
            } catch (MessageException e) {
                System.out.println(e.getMessage());
            }
        }
        LocalDateTime checkOutDate = dateTime == null ? null : dateTime.plusHours(hour);
        return BookingDTO.builder()
                .roomId(roomId)
                .paymentStatus(payTypeVO.getName())
                .checkInDate(dateTime)
                .checkOutDate(checkOutDate)
                .build();
    }

    @Override
    public Long delete() {
        System.out.println("请输入要删除的预订id:");
        return ScannerUtils.sc.nextLong();
    }

    @Override
    public BookingDTO update() {
        System.out.println("请输入要修改的预订编号:");
        long bookingId = ScannerUtils.sc.nextLong();
        System.out.println("请输入预订的房间id:");
        long roomId = ScannerUtils.sc.nextLong();
        System.out.println("请输入预订人的id:");
        long guestId = ScannerUtils.sc.nextLong();
        System.out.println("支付方式包括如下几种:");
        PayTypeVO payTypeVO;
        while (true) {
            payService.list();
            System.out.println("请输入预订人的付款方式:");
            payTypeVO = payService.getByName(ScannerUtils.sc.next());
            if (payTypeVO == null) {
                System.out.println(CommonConstant.COMMAND_ERROR);
            } else {
                break;
            }
        }
        System.out.println("请输入本次的预订总价:");
        double price = ScannerUtils.sc.nextDouble();
        LocalDateTime checkInDate = LocalDateTime.now();
        System.out.println("请输入预订人要租住几个小时:");
        int hour = ScannerUtils.sc.nextInt();
        LocalDateTime checkOutDate = checkInDate.plusHours(hour);
        return BookingDTO.builder()
                .id(bookingId)
                .roomId(roomId)
                .guestId(guestId)
                .paymentStatus(payTypeVO.getName())
                .totalPrice(price)
                .checkInDate(checkInDate)
                .checkOutDate(checkOutDate)
                .build();
    }
}
